extends Node2D
class_name BaseBullet

const _pre_hit_effect = preload("res://scene/HitEffect.tscn")

var speed = GameProperties.get_weapon_property("bullet_speed") # 子弹速度
var penetrating_bullet =GameProperties.get_weapon_property("penetrating_bullet")

var is_crit = GameProperties.get_weapon_property("crit_rate") < randf_range(0,1)

@export var dir = Vector2.ZERO # 子弹飞行向量

var start_position: Vector2 # 子弹的起始位置

var current_weapon:BaseWeapon # 属于某个武器
var bullet_range =GameProperties.get_weapon_property("bullet_range")
#var _tick = 0 

func _ready() -> void:
	start_position = global_position
	look_at(get_global_mouse_position())

func _physics_process(delta: float) -> void:
	global_position += dir * delta * speed

	# 如果子弹的移动距离超过了射程，销毁子弹
	if global_position.distance_to(start_position) >= bullet_range:
		queue_free()

	#_tick += delta
	#if Engine.get_physics_frames() % 20 :
	#	if _tick >= 3:
	#		queue_free()

func _on_area_2d_body_entered(body: Node2D) -> void:
	if body is BaseEnemy:
		Game.damage(Game.player,body,is_crit)
		penetrating_bullet -=1
		if penetrating_bullet >0:
			return
		set_physics_process(false)
		var ins = _pre_hit_effect.instantiate()
		ins.global_position = global_position
		Game.map.add_child(ins)
		queue_free()
